package dao

import (
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/bulletin"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type BulletinDao struct {
}

func NewBulletinDao() *BulletinDao {
	return &BulletinDao{}
}

// InsertAppStaffInformMessage 写入员工消息
func (dao *BulletinDao) InsertAppStaffInformMessage(param []*bulletin.StaffInformMessageDb) *dingo.ErrorWrap {
	db := global.Orm.Create(&param)
	return dingo.ErrorIf(db.Error, true)
}

// SetAppStaffInformMessageStatus 员工消息状态更新
func (dao *BulletinDao) SetAppStaffInformMessageStatus(param *request.AppGetStaffInformMessageListReq) *dingo.ErrorWrap {
	db := global.Orm.Table("staff_inform_message sim")
	db.Where("sim.staff_id=? AND sim.place_id=?", param.LoginUserId, param.PlaceId)

	if len(param.Ids) > 0 {
		db.Where("sim.id IN ?", param.Ids)
	}

	// "sim.status", helper.TypeNumCommonStatusTrue
	db.Updates(map[string]interface{}{
		"status":    helper.TypeNumCommonStatusTrue,
		"read_time": time.Now().Format("2006-01-02 15:04:05"),
	})
	return dingo.ErrorIf(db.Error, true)
}

// GetAppStaffInformMessageList 员工消息列表
func (dao *BulletinDao) GetAppStaffInformMessageList(param *request.AppGetStaffInformMessageListReq) *dingo.ErrorWrap {
	var (
		lst    []*bulletin.StaffInformMessage
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `id, staff_id, place_id, status, type, title, message, DATE_FORMAT(read_time,'%Y-%m-%d %T')read_time,
	DATE_FORMAT(create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("staff_inform_message sim").Select(fields)
	db.Where("sim.staff_id=? AND sim.place_id=?", param.LoginUserId, param.PlaceId)

	if param.Type > 0 {
		db.Where("sim.type=?", param.Type)
	}
	if param.Status > 0 {
		db.Where("sim.status=?", param.Status)
	}

	db.Order("sim.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// SetStewardPlaceBulletinClose 关闭公告
func (dao *BulletinDao) SetStewardPlaceBulletinClose(param *request.DbBulletinReq) *dingo.ErrorWrap {
	updateData := map[string]interface{}{
		"status": helper.TypeNumBulletinStatusClose,
	}
	db := global.Orm.Model(&param.Db).Updates(updateData)
	return dingo.ErrorIf(db.Error)
}

// GetAdminBulletinById 根据ID查询公告
func (dao *BulletinDao) GetAdminBulletinById(param *request.DbBulletinReq) *dingo.ErrorWrap {
	info := &bulletin.Bulletin{}

	db := global.Orm.Table("bulletin b").Select("b.*")
	db.Joins("LEFT JOIN places p ON b.place_id=p.id")
	db.Where("b.id=?", param.Db.Id)

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	db.Find(&info)
	return dingo.ErrorIf(db.Error, info)
}

// SetAdminBulletinStatus 公告审核
func (dao *BulletinDao) SetAdminBulletinStatus(param *request.DbBulletinReq) *dingo.ErrorWrap {
	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		return dingo.ErrorIf(nil)
	}

	info := dao.GetAdminBulletinById(param).Unwrap().(*bulletin.Bulletin)
	if info.Id == 0 || info.Status != helper.TypeNumBulletinStatusWait {
		return dingo.ErrorIf(nil)
	}

	updateData := map[string]interface{}{
		"status":         param.Db.Status,
		"reject_message": param.Db.RejectMessage,
	}
	db := global.Orm.Model(&param.Db).Where("id=?", param.Db.Id).Updates(updateData)
	return dingo.ErrorIf(db.Error)
}

// DeleteAdminBulletinList 删除公告
func (dao *BulletinDao) DeleteAdminBulletinList(param *request.DbBulletinReq) *dingo.ErrorWrap {
	db := global.Orm.Model(&param.Db).Delete("id=?", param.Db.Id)
	return dingo.ErrorIf(db.Error)
}

// UpdateAdminBulletinList 修改公告
func (dao *BulletinDao) UpdateAdminBulletinList(param *request.DbBulletinReq) *dingo.ErrorWrap {
	param.Db.Status = helper.TypeNumBulletinStatusOpen
	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		param.Db.Status = helper.TypeNumBulletinStatusWait
	}
	db := global.Orm.Where("id=?", param.Db.Id).Updates(&param.Db)
	return dingo.ErrorIf(db.Error)
}

// InsertAdminBulletinList 新增公告
func (dao *BulletinDao) InsertAdminBulletinList(param *request.DbBulletinReq) *dingo.ErrorWrap {
	param.Db.Status = helper.TypeNumBulletinStatusOpen
	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		param.Db.Status = helper.TypeNumBulletinStatusWait
	}
	db := global.Orm.Create(&param.Db)
	return dingo.ErrorIf(db.Error)
}

// GetAdminBulletinList 公告列表
func (dao *BulletinDao) GetAdminBulletinList(param *request.BulletinListReq) *dingo.ErrorWrap {
	var (
		lst    []*request.BulletinListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `b.id,b.place_id,b.status,weight,title,message,p.place_name,reject_message,DATE_FORMAT(expire_time,'%Y-%m-%d %T')
	expire_time,DATE_FORMAT(create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("bulletin b").Select(fields)
	db.Joins("LEFT JOIN places p ON b.place_id=p.id")

	if param.IsAdmin != helper.TypeNumIsAdminOperator {
		db.Where("p.user_id=?", param.LoginUserId)
	}

	if param.Status > 0 {
		db.Where("b.status= ?", param.Status)
	}
	if param.PlaceId > 0 {
		db.Where("b.place_id= ?", param.PlaceId)
	}
	if param.PlaceId < 0 {
		db.Where("b.place_id= ?", 0)
	}
	if param.Title != "" {
		db.Where("b.title LIKE ?", "%"+param.Title+"%")
	}

	db.Order("b.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

// GetWxBulletinList 公告列表
func (dao *BulletinDao) GetWxBulletinList(param *request.BulletinListReq, isExpire bool) *dingo.ErrorWrap {
	var (
		lst    []*request.BulletinListRes
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)

	fields := `b.id,b.place_id,b.status,weight,title,message,p.place_name,reject_message,DATE_FORMAT(expire_time,'%Y-%m-%d %T')
	expire_time,DATE_FORMAT(create_time,'%Y-%m-%d %T')create_time,DATE_FORMAT(update_time,'%Y-%m-%d %T')update_time`
	db := global.Orm.Table("bulletin b").Select(fields)
	db.Joins("LEFT JOIN places p ON b.place_id=p.id")
	db.Where("(b.place_id=? OR b.place_id=0) AND b.status=?", param.PlaceId, helper.TypeNumBulletinStatusOpen)

	if isExpire {
		db.Where("b.expire_time>now()")
	}

	db.Order("b.id DESC")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}
